//
//  BdjlVC.swift
//  Auscultation
//
//  Created by 来 on 2020/12/11.
//

import UIKit

class BdjlVC: BaseVC {
    
    var collectionView: UICollectionView!
    
    var dataArray = UserDefaults.standard.array(forKey: LGF_BD_WAV_ARRAY) ?? []

    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
    }

}

extension BdjlVC : UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize  {
        return CGSize.init(width: UIDevice.lgf_ScreenW, height: 80)
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.lgf_DequeueReusableCell(with: BdjlCell.self, for: indexPath)
        if let data = dataArray[indexPath.item] as? [String: String] {
//            cell.xm.text = "姓名：" + data["xm"]!
//            cell.nl.text = "年龄：" + data["nl"]!
//            cell.xb.text = "性别：" + data["xb"]!
            cell.tzyb.text = data["name"]
            cell.bdjl_delete = { [weak self] in
                self?.dataArray.forEach { (data1) in
                    if let data2 = data1 as? [String: String] {
                        let tzyb = data2["name"]!
                        if data["name"] == tzyb {
                            self?.removePcm(LGF_Documents_PCM + data2["pcm_path"]!)
                            self?.removePcm(LGF_Documents_WAV + data2["wav_path"]!)
                            self?.dataArray.remove(at: indexPath.item)
                            UserDefaults.standard.setValue(self?.dataArray, forKey: LGF_BD_WAV_ARRAY)
                            self?.collectionView.reloadData()
                            return
                        }
                    }
                }
            }
        }
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let data = dataArray[indexPath.item] as? [String: String] {
            let vc = BdjlxqVC()
            vc.data = data
            self.navigationController?.show(vc, sender: self)
        }
    }
}
